//
//  HasInstalledPowerViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/7/13.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "HasInstalledPowerViewController.h"
#import "HasInstalledPowerTableViewCell.h"
#import "HasInstalledPowerTableViewCell2.h"

#import "BGItemsViewController.h"

#import "SortJumpViewController.h"

#define kCellName @"HasInstalledPowerTableViewCell"
#define kCellName2 @"HasInstalledPowerTableViewCell2"


#define kPageSize @"20"

@interface HasInstalledPowerViewController ()<UITableViewDelegate,UITableViewDataSource,SortJumpViewControllerDelegate>
{
    NSString *_sortString ;
    NSInteger _pageIndex;
}
@property (nonatomic,strong) NSMutableArray *dataArray;


@end

@implementation HasInstalledPowerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self UISetting];
    [self loadData];
}

- (void)UISetting{
    if (_defaultValueNUM.integerValue == 0) {
        _defaultValueNUM = [NSNumber numberWithInteger:1];
    }else{
        _defaultValueNUM = [NSNumber numberWithInteger:0];
    }
    if (_zaixianNUM.integerValue == 0) {
        _zaixianNUM = [NSNumber numberWithBool:0];
    }
    if (_dianliangNUM.integerValue == 0) {
        _dianliangNUM = [NSNumber numberWithBool:0];
    }
    if (_antianNUM.integerValue == 0) {
        _antianNUM = [NSNumber numberWithBool:0];
    }
    if (_leixingNUM.integerValue == 0) {
        _leixingNUM = [NSNumber numberWithBool:0];
    }

    _dataArray = [NSMutableArray array];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageIndex = 0;
        [self loadData];
        [_tableView.mj_header endRefreshing];
    }];
    
    _tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _pageIndex ++;
        [self loadData];
        [_tableView.mj_footer endRefreshing];

    }];
}

#pragma mark - 网络请求
- (void)loadData{
    [self.hub hideAnimated:YES];
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    // Set the label text.
    self.hub.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    NSString *pageIndexString = [NSString stringWithFormat:@"%ld",_pageIndex];
    
    NSString *electricity_sc_status;
    NSString *on_line_sc_status;
    NSString *on_line;
    NSString *dump_energy_per;
    NSString *offline_day;
    NSString *device_type;
    
    //如果默认情况，所有参数均为空
    if (_defaultValueNUM.integerValue == 1) {
        electricity_sc_status = nil;
        on_line_sc_status = nil;
        on_line = nil;
        dump_energy_per = nil;
        offline_day = nil;
        device_type = nil;
    }else{
        if (_zaixianNUM.integerValue >= 100) {
            on_line = [NSString stringWithFormat:@"%ld",_zaixianNUM.integerValue - 101];
        }
        if (_antianNUM.integerValue > 300) {
            offline_day = [NSString stringWithFormat:@"%ld",_antianNUM.integerValue - 300];
        }
        if (_dianliangNUM.integerValue == 206) {//从低到高
            electricity_sc_status = @"asc";
        }else if (_dianliangNUM.integerValue == 207){//从高到低
            electricity_sc_status = @"desc";
        }else{
            if (_dianliangNUM.integerValue > 200) {
                dump_energy_per = [NSString stringWithFormat:@"%ld",_dianliangNUM.integerValue - 200];
            }
        }
        
        if (_leixingNUM.integerValue == 501) {//wifi
            device_type = @"1";
        }else if(_leixingNUM.integerValue == 502){//密码
            device_type = @"4";
        }
    }
    
    [[AFClient shareInstance] hasInstalledPowerByEid:kUserId page_size:kPageSize page_index:pageIndexString electricity_sc_status:electricity_sc_status on_line_sc_status:nil on_line:on_line dump_energy_per:dump_energy_per offline_day:offline_day device_type:device_type progressBlock:nil success:^(id responseBody) {
        [self.hub hideAnimated:YES];
        if ([responseBody[@"code"] integerValue] == 200) {
            if (_pageIndex == 0) {
                [_dataArray removeAllObjects];
            }
            _labelPowerCount.text = [NSString stringWithFormat:@"当前筛选条件下的充电宝总数：%@",responseBody[@"total"]];
            for (NSDictionary *dict in responseBody[@"data"]) {
                HasInstalledModel *model = [[HasInstalledModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                model.store_info = dict[@"store_info"];
                [_dataArray addObject:model];
            }
            [self creatImageBackView:_dataArray];
            [_tableView reloadData];
        }else{
            kAlert(responseBody[@"message"]);
        }
    } failure:^(NSError *error) {
        [self.hub hideAnimated:YES];
    }];
}

#pragma mark - 点击事件
//返回
- (IBAction)backButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//排序
- (IBAction)sortButtonClick:(UIButton *)sender {
    [self sortNewFunc];
}

- (void)sortNewFunc{
    UIStoryboard *storyboard = kMainStroyboard;
    SortJumpViewController *sortJumpVC = [storyboard instantiateViewControllerWithIdentifier:@"SortJumpViewController"];
    sortJumpVC.defaultValueNUM = _defaultValueNUM;
    sortJumpVC.zaixianNUM = _zaixianNUM.integerValue >=0 ? _zaixianNUM : [NSNumber numberWithInteger:0];
    sortJumpVC.dianliangNUM = _dianliangNUM.integerValue >=0 ? _dianliangNUM : [NSNumber numberWithInteger:0];;
    sortJumpVC.antianNUM = _antianNUM.integerValue >=0 ? _antianNUM : [NSNumber numberWithInteger:0];
    sortJumpVC.leixingNUM = _leixingNUM.integerValue >=0 ? _leixingNUM : [NSNumber numberWithInteger:0];

    sortJumpVC.delegate = self;
    sortJumpVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:sortJumpVC animated:YES completion:nil];
}


#pragma mark - tableViewDelegate & dataSource
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HasInstalledModel *model = _dataArray[indexPath.row];
    if (model.device_type.integerValue == 1) {//wifi 充电宝
        HasInstalledPowerTableViewCell *cell = (HasInstalledPowerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kCellName];
        cell.labelIndex.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
        [cell showDataWithModel:model];
        
        return cell;
    }else{
        HasInstalledPowerTableViewCell2 *cell = (HasInstalledPowerTableViewCell2 *)[tableView dequeueReusableCellWithIdentifier:kCellName2];
        cell.labelIndex.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
        [cell showDataWithModel:model];
        
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        return 220;
}

#pragma mark - sortJumpViewDelegate

- (void)getSortJumpInfoWithDefault:(NSNumber *)defaultValue zaixian:(NSNumber *)zaixian dianliang:(NSNumber *)dianliang antian:(NSNumber *)antian leixing:(NSNumber *)leixing{
    _defaultValueNUM = defaultValue;
    _zaixianNUM = zaixian;
    _dianliangNUM = dianliang;
    _antianNUM = antian;
    _leixingNUM = leixing;
    _pageIndex = 0;
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
